package org.i_plus.model.dao;

import java.util.Iterator;

import org.i_plus.model.BonusBean;
import org.i_plus.model.BonusDAO;



import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import antlr.DefaultFileLineFormatter;

public class BonusBeanHibernateDAO implements BonusDAO {
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	@Override
	public void selectB(int num){
		Session session =this.sessionFactory.getCurrentSession();
		BonusBean bean = (BonusBean) session.get(BonusBean.class, num);
		System.out.println(bean.getAccount());
		System.out.println(bean.getBtime());
		System.out.println(bean.getHowmuch());
		//session.close();
	
	}
	@Override
	public void insertB(String account,java.sql.Timestamp btime,int howmuch){
		Session session =this.sessionFactory.getCurrentSession();
//		Session session = DefaultFactory.getSessionFactory().openSession();
//		Transaction tx = session.beginTransaction();
		BonusBean bean = new BonusBean();
		bean.setAccount(account);
		bean.setBtime(btime);
		bean.setHowmuch(howmuch);
		session.save(bean);
//		tx.commit();
//		session.close();
	}
	@Override
	public void updateB(String account,java.sql.Timestamp btime,int howmuch,int num){
		Session session =this.sessionFactory.getCurrentSession();
		//Transaction tx = session.beginTransaction();
		BonusBean bean = (BonusBean)session.get(BonusBean.class, num);
		bean.setAccount(account);
		bean.setBtime(btime);
		bean.setHowmuch(howmuch);
		session.saveOrUpdate(bean);
		//tx.commit();session.close();
	}
	@Override
	public void deleteB(int num){
		Session session =this.sessionFactory.getCurrentSession();
		//Transaction tx = session.beginTransaction();
		BonusBean bean =(BonusBean) session.get(BonusBean.class, num);
		session.delete(bean);
		//tx.commit();session.close();
	}
	
	public static void main(String[] args){
		
		ApplicationContext context =
				new ClassPathXmlApplicationContext("beans.config.xml");
		SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		
		Transaction tx = sessionFactory.getCurrentSession().beginTransaction();
		
		BonusBeanHibernateDAO bbdao = (BonusBeanHibernateDAO) context.getBean("bonusDAO");
		bbdao.insertB("123", new java.sql.Timestamp(0, 0, 0, 0, 0, 0, 0), 50);
		//bbdao.selectB(7);
		//bbdao.updateB("123", new java.sql.Timestamp(0, 0, 0, 0, 0, 0, 1), 23, 7);
//		bbdao.deleteB(7);
		System.out.println("done");
		
		tx.commit();
		
		
		
//		BonusBeanHibernateDAO bbdao = new BonusBeanHibernateDAO();
//		bbdao.insertB("123", new java.sql.Timestamp(0, 0, 0, 0, 0, 0, 0), 30);
//		bbdao.selectB(1);
//		bbdao.updateB("456", new java.sql.Timestamp(0, 0, 0, 0, 0, 0, 0), 40, 1);
//		bbdao.deleteB(1);
//		System.out.println("done");
	}
}
